package com.chenyun.controller;

import com.chenyun.pojo.Result;
import com.chenyun.pojo.User;
import com.chenyun.service.impl.UserService;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        /*//1.注册接口参数校验，普通方法
        if (username!=null&&username.length()>=5&&username.length()<=16&&password!=null&&password.length()>=5&&password.length()<=16)
        {
            //查询
            User user=userService.selectByUserName(username);
            if (user==null){
                //没有占用
                userService.register(username,password);
                return Result.success();
            }else {
                //占用
                return Result.error("用户名已被占用");
            }
            //注册
        }else {
            return Result.error("参数不合法");

        }*/
        //2.spring validation   @Pattern(regexp = "^\\S{5,16}$")
        //查询
        User user = userService.selectByUserName(username);
        if (user == null) {
            //没有占用
            userService.register(username, password);
            return Result.success();
        } else {
            //占用
            return Result.error("用户名已被占用");
        }
        //注册

    }
}
